package com.example.myfivechess.gameviews;

import com.example.myfivechess.R;
import com.example.myfivechess.R.drawable;
import com.example.myfivechess.utils.AssetsLoad;
import com.example.myfivechess.utils.Constant;
import com.example.myfivechess.utils.Utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.widget.Toast;

public class MyGameView extends SurfaceView implements Runnable, Callback {
	private SurfaceHolder sfh;
	private Canvas canvas;
	private Thread th = new Thread(this);
	private boolean isStop = false;
	private Paint paint;
	private Resources res;
	private Bitmap whiteMap;
	private Bitmap blackMap;
	private boolean WHITE_FLAG = true;
	private Bitmap woodBackground;
	private int whoWin = 0;// 0没有，1白色win，2黑色win

	public MyGameView(Context context) {
		super(context);
		sfh = this.getHolder();
		sfh.addCallback(this);
		setFocusable(true);
		res = getResources();
		whiteMap = BitmapFactory.decodeResource(res, R.drawable.human);
		blackMap = BitmapFactory.decodeResource(res, R.drawable.ai);
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setColor(Color.BLACK);
		woodBackground = BitmapFactory.decodeResource(this.getContext()
				.getResources(), R.drawable.wood_background);

		// TODO Auto-generated constructor stub
	}

	// 触摸事件
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		if (event.getAction() == MotionEvent.ACTION_DOWN)
			if (!isStop) {
				for (int y = 0; y < 9; y++) {
					for (int x = 0; x < 9; x++) {
						if (Constant.ground[y][x] == 0
								&& isInCircle(event.getX(), event.getY(), x, y)) {
							if (WHITE_FLAG) {
								Constant.ground[y][x] = Constant.WHITE_CHESS;
								AssetsLoad.playSound(getContext(),
										AssetsLoad.putSoundId);
								// ai在此处落子
								if (Utils.isWin(x, y)) {
									draw();
									whoWin = 1;
									Toast.makeText(getContext(), "白棋获胜",
											Toast.LENGTH_SHORT).show();
									isStop = true;

								}
							} else {
								Constant.ground[y][x] = Constant.BLACK_CHESS;
								AssetsLoad.playSound(getContext(),
										AssetsLoad.putSoundId);
								if (Utils.isWin(x, y)) {
									draw();
									whoWin = 2;
									Toast.makeText(getContext(), "黑棋获胜",
											Toast.LENGTH_SHORT).show();
									isStop = true;
								}

							}

							WHITE_FLAG = !WHITE_FLAG;
							return super.onTouchEvent(event);
						}
					}
				}
			}

		return super.onTouchEvent(event);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		th.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		Utils.initGroup();
		isStop = true;
	}

	// 判断是否与某点最近
	private boolean isInCircle(float touch_x, float touch_y, int x, int y) {

		return ((touch_x - ((x + 1) * 2 * Constant.RECT_R))
				* (touch_x - ((x + 1) * 2 * Constant.RECT_R)) + (touch_y - ((y + 1) * 2 * Constant.RECT_R))
				* (touch_y - ((y + 1) * 2 * Constant.RECT_R))) < Constant.RECT_R
				* Constant.RECT_R;
	}

	private void draw() {
		try {
			canvas = sfh.lockCanvas();
			// canvas.drawBitmap(woodBackground,0,0,null);
			canvas.drawBitmap(woodBackground, null, new RectF(0, 0,
					Constant.SCREENWIDTH, Constant.SCREENHEIGHT), null);
			// 横线
			for (int i = 0; i < 9; i++) {
				canvas.drawLine(2 * Constant.RECT_R, 2 * Constant.RECT_R * i
						+ 2 * Constant.RECT_R, 9 * 2 * Constant.RECT_R, 2
						* Constant.RECT_R * i + 2 * Constant.RECT_R, paint);
			}
			for (int j = 0; j < 9; j++) {
				canvas.drawLine(2 * Constant.RECT_R + 2 * Constant.RECT_R * j,
						2 * Constant.RECT_R, 2 * Constant.RECT_R + 2
								* Constant.RECT_R * j, 9 * 2 * Constant.RECT_R,
						paint);
			}
			for (int y = 0; y < 9; y++) {
				for (int x = 0; x < 9; x++) {
					if (Constant.ground[y][x] != 0)
						drawMyBitmap(x, y);
					// 这里加入判断是否有人获胜
				}
			}
			if (whoWin == 0) {

				if (WHITE_FLAG)
					canvas.drawBitmap(AssetsLoad.picWhiteTurn, null, new RectF(
							0, 10 * 2 * Constant.RECT_R, Constant.SCREENWIDTH,
							Constant.SCREENHEIGHT), null);
				else {
					canvas.drawBitmap(AssetsLoad.picBlackTurn, null, new RectF(
							0, 10 * 2 * Constant.RECT_R, Constant.SCREENWIDTH,
							Constant.SCREENHEIGHT), null);

				}
			} else {
				if (whoWin == 1) {
					canvas.drawBitmap(AssetsLoad.picWhiteWin, null, new RectF(
							0, 10 * 2 * Constant.RECT_R, Constant.SCREENWIDTH,
							Constant.SCREENHEIGHT), null);
				} else {
					canvas.drawBitmap(AssetsLoad.picBlackWin, null, new RectF(
							0, 10 * 2 * Constant.RECT_R, Constant.SCREENWIDTH,
							Constant.SCREENHEIGHT), null);

				}

			}

		} catch (Exception e) {
			System.out.println(e.toString());
		} finally {
			if (canvas != null) {
				sfh.unlockCanvasAndPost(canvas);
			}
		}
	}

	// 以某点为中心，画图片上去
	private void drawMyBitmap(int x, int y) {
		if (Constant.ground[y][x] == Constant.WHITE_CHESS)
			canvas.drawBitmap(whiteMap, ((x + 1) * 2 * Constant.RECT_R)
					- Constant.CHESS_R, ((y + 1) * 2 * Constant.RECT_R)
					- Constant.CHESS_R, null);
		else {
			canvas.drawBitmap(blackMap, ((x + 1) * 2 * Constant.RECT_R)
					- Constant.CHESS_R, ((y + 1) * 2 * Constant.RECT_R)
					- Constant.CHESS_R, null);
		}
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (!isStop) {
			draw();
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
